package com.lsa.southwind.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lsa.southwind.entity.SystemAdmin;
import com.lsa.southwind.form.RuleForm;
import com.lsa.southwind.mapper.SystemAdminMapper;
import com.lsa.southwind.service.SystemAdminService;
import com.lsa.southwind.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author admin
 * @since 2023-02-25
 */
@Service
public class SystemAdminServiceImpl extends ServiceImpl<SystemAdminMapper, SystemAdmin> implements SystemAdminService {
    @Autowired
    private SystemAdminMapper systemAdminMapper;

    @Override
    public ResultVo login(RuleForm ruleForm) {
        //1.判读用户名是否存在
        //使用querywrapper去判断
        QueryWrapper<SystemAdmin> queryWrapper = new QueryWrapper<>();
        //这里.eq("数据库表的列名","要比较的值")
        queryWrapper.eq("username", ruleForm.getUsername());
        SystemAdmin systemAdmin = this.systemAdminMapper.selectOne(queryWrapper);
        ResultVo resultVo = new ResultVo();
        if (systemAdmin == null) {
            //如果查询出来为空,说明用户不存在
            resultVo.setCode(-1);
        } else {
            //2.判断密码所示否正确
            if (!systemAdmin.getPassword().equals(ruleForm.getPassword())) {
                resultVo.setCode(-2);
            } else {
                resultVo.setCode(0);
                resultVo.setData(systemAdmin);
            }
        }
        return resultVo;
    }
}
